package com.binance.mbxgateway.web.service.impl;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import com.binance.account.api.UserApi;
import com.binance.account.api.UserInfoApi;
import com.binance.assetservice.api.IProductApi;
import com.binance.assetservice.api.ITranApi;
import com.binance.assetservice.api.IUserAssetApi;
import com.binance.assetservice.vo.request.product.GetProductItemRequest;
import com.binance.assetservice.vo.response.product.ProductItemResponse;
import com.binance.master.error.BusinessException;
import com.binance.master.error.GeneralCode;
import com.binance.mbxgateway.web.service.ITradeService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class TradeServiceImpl extends BaseServiceImpl implements ITradeService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${matchbox.rest.root.url}")
    private String restUrl;

    @Resource
    private IProductApi productApi;

    // @Autowired
    // private IMatchboxService matchboxService;

    @Resource
    private UserApi userApi;
    @Resource
    private UserInfoApi userInfoApi;

    @Resource
    private ITranApi tranApi;
    @Resource
    private IUserAssetApi userAssetApi;

    @Override
    public String getAggTrades(String symbol, Long startTime, Long endTime, Long limit) throws Exception {
        // To Do 要通过productServide判断symbol是否存在
        GetProductItemRequest body = new GetProductItemRequest();
        body.setSymbol(symbol);
        ProductItemResponse productResponse =
                this.getAPIRequestResponse(productApi.getProductItem(this.newAPIRequest(body)));
        if (productResponse == null || productResponse.getProductItems().isEmpty()) {
            throw new BusinessException(GeneralCode.SYS_ERROR, "产品不存在");
        }
        UriComponentsBuilder builder =
                UriComponentsBuilder.fromHttpUrl(String.format("%s/aggTrades", restUrl)).queryParam("symbol", symbol);
        if (startTime != null) {
            builder.queryParam("startTime", startTime);
        }
        if (endTime != null) {
            builder.queryParam("endTime", endTime);
        }
        if (limit != null) {
            builder.queryParam("limit", limit);
        }
        try {
            String json = this.restTemplate.getForObject(builder.toUriString(), String.class);
            return json;
        } catch (HttpStatusCodeException e) {
            String json = e.getResponseBodyAsString();
            log.error("getAggTrades is error:{},{}", json, e);
            throw new BusinessException(json);
        } catch (Exception e) {
            log.error("getAggTrades exception: {}", e);
            throw e;
        }
    }

    // @Override
    // public void backfillTrades(String symbol, Long tradeId) throws Exception {
    // String json = this.matchboxService.getTrades(symbol, null, tradeId, 1, null);
    // JSONArray array = JSON.parseArray(json);
    // if (array.isEmpty()) {
    // return;
    // }
    // JSONObject tradeObject = array.getJSONObject(0);
    // ProcessTradeForBackfillTradesRequest request = new ProcessTradeForBackfillTradesRequest();
    // request.setSymbol(symbol);
    // request.setTradeId(tradeId);
    // request.setTradeObject(tradeObject.toJSONString());
    // this.getAPIRequestResponse(this.userAssetApi.processTradeForBackfillTrades(this.newAPIRequest(request)));
    // }

}
